﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class HLTVV06Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b; 
        string WBBAUFKRS;
        string WBBAUFLFD;
        string WBBAUFPOS;
        string WBBALIEFSCH;
        string VKAUFNum = "";
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public HLTVV06Report()
            : base()
        {
        }
        public HLTVV06Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            WBBAUFKRS = dicParamater["WBBAUFKRS"].ToString().Trim();
            WBBAUFLFD = dicParamater["WBBAUFLFD"].ToString().Trim();
            WBBAUFPOS = dicParamater["WBBAUFPOS"].ToString().Trim();
            VKAUFNum = WBBAUFKRS +"-"+ WBBAUFLFD +"-"+WBBAUFPOS;
            WBBALIEFSCH = dicParamater["WBBALIEFSCH"].ToString().Trim();
            WBBTAX = dicParamater["WBBTAX"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() +"年"+ DateTime.Now.Month.ToString() +"月"+ DateTime.Now.Day.ToString()+"日";
        }
        #region------------------------------Function------------------------ 
        private void FillInWBBST(DataTable dtSource)
        {
            try
            {
                string WBBSTKNR = "";
                string WBBSSTKLG = "";
                DataSet setTemp = new DataSet();
                int x=0,startx = 10;
                int y = 2;
                int page = 0,runs=0;
                int rowsperpage=25;
                int pagestart = 10;
                decimal subtotalqty = 0,totalqty=0;
                int subtotalpcnum = 0,totalpcnum=0;
                x = startx;
                pagestart = (page) * rowsperpage + startx;
                string expression="";
                string subConclusion = "";
                string Conclusion = "";
                string VVARTICLE = "";
                bool flgBreak=false;
                decimal qty = 0;
                foreach (string Color in ListColor)
                {
                    foreach (string BatchNo in listBatchNo)
                    { 
                        setTemp = new DataSet();
                        expression = "WBBTAX='"+Color+"' and WBBDPART='"+BatchNo+"'";
                        setTemp.Merge(dtSource.Select(expression));
                        #region FillExcel
                        if(setTemp.Tables.Count==0)
                            continue;
                        foreach (DataRow dwSource in setTemp.Tables[0].Rows)
                        {
                            //subtotalpcnum++;
                            VVARTICLE = "";
                            VVARTICLE+= dwSource["WBBART"].ToString().Trim();
                            VVARTICLE+="-"+ dwSource["WBBFARBE"].ToString().Trim();
                            VVARTICLE+= "-"+dwSource["WBBVAR"].ToString().Trim();
                            #region if(x == 10)
                            if (x == 10)
                            {  
                                int yx = y;
                                if (x == 10)
                                {
                                    yx = 5;
                                    exAgent.SetCellValue(pagestart - 5, 5, Customer);
                                    exAgent.SetCellValue(pagestart - 5, 19, DateCurrent);
                                    exAgent.SetCellValue(pagestart - 4, 5, Address);
                                    
                                    exAgent.SetCellValue(pagestart - 7, 22, VKAUFNum);
                                    exAgent.SetCellValue(pagestart - 6, 22, WBBALIEFSCH);
                                }
                                exAgent.SetCellValue(pagestart - 3, yx, VVARTICLE);
                                exAgent.SetCellValue(pagestart - 1, yx, Color);
                                exAgent.SetCellValue(pagestart - 2, yx, BatchNo);
                            }
                            #endregion
                            if (flgBreak&&runs>=2)
                            {
                                flgBreak = false;
                                runs = 0;
                                page++;
                                x = (page) * rowsperpage + startx;
                                y = 2;
                                int j = 5;
                                pagestart = (page) * rowsperpage + startx;
                                exAgent.SetCellValue(pagestart - 5, 5, Customer);
                                exAgent.SetCellValue(pagestart - 5, 19, DateCurrent);
                                exAgent.SetCellValue(pagestart - 4, 5, Address);
                                exAgent.SetCellValue(pagestart - 7, 22, VKAUFNum);
                                exAgent.SetCellValue(pagestart - 6, 22, WBBALIEFSCH);

                                exAgent.SetCellValue(pagestart - 3, j, VVARTICLE);
                                exAgent.SetCellValue(pagestart - 1, j, Color);
                                exAgent.SetCellValue(pagestart - 2, j, BatchNo);
                            }
                            if (flgBreak && runs<2)
                            {
                                flgBreak = false;
                                runs = 2;
                                x = (page) * rowsperpage + startx;
                                y = 12;
                                int j = 12;
                                pagestart = (page) * rowsperpage + startx;

                                exAgent.SetCellValue(pagestart - 3, j, VVARTICLE);
                                exAgent.SetCellValue(pagestart - 1, j, Color);
                                exAgent.SetCellValue(pagestart - 2, j, BatchNo);
                            }
                            WBBSTKNR = dwSource["WBBSTKNR"].ToString().Trim();
                            WBBSSTKLG = dwSource["WBBSSTKLG"].ToString().Trim();
                            qty = System.Math.Round(System.Convert.ToDecimal(WBBSSTKLG), 1);
                            //subtotalqty += qty;
                            x += 1;
                            if (x - pagestart == 11)
                            {
                                
                                x = pagestart + 1;
                                y += 5;
                                runs++;
                               /* if (runs == 2)
                                {
                                    //subConclusion = subtotalpcnum + "匹" + subtotalqty + "码";
                                    //exAgent.SetCellValue(pagestart + 11, 5, subConclusion);
                                    totalqty += subtotalqty;
                                    totalpcnum += subtotalpcnum;
                                    subtotalpcnum = 0;
                                    subtotalqty = 0;
                                }*/
                                /*if (runs == 4)
                                {
                                    totalqty += subtotalqty;
                                    totalpcnum += subtotalpcnum;
                                    //subConclusion = subtotalpcnum + "匹" + subtotalqty + "码";
                                    //Conclusion = totalpcnum + "匹" + totalqty + "码";
                                    //exAgent.SetCellValue(pagestart + 11, 14, subConclusion);
                                    //exAgent.SetCellValue(pagestart + 12, 5, Conclusion); 
                                    subtotalpcnum = 0;
                                    subtotalqty = 0;
                                }*/
                                if (runs == 4)
                                {
                                    runs = 0;
                                    page++;
                                    x = (page) * rowsperpage + startx + 1;
                                    y = 2;
                                    int i = 5;
                                    pagestart = (page) * rowsperpage + startx;
                                    exAgent.SetCellValue(pagestart - 5, 5, Customer);
                                    exAgent.SetCellValue(pagestart - 5, 19, DateCurrent);
                                    exAgent.SetCellValue(pagestart - 4, 5, Address);
                                    exAgent.SetCellValue(pagestart - 7, 22, VKAUFNum);
                                    exAgent.SetCellValue(pagestart - 6, 22, WBBALIEFSCH);
                                    
                                    exAgent.SetCellValue(pagestart - 3, i, VVARTICLE);
                                    exAgent.SetCellValue(pagestart - 1, i, Color);
                                    exAgent.SetCellValue(pagestart - 2, i, BatchNo);
                                    
                                }
                               
                            }
                            
                            exAgent.SetCellValue(x, y, WBBSTKNR);
                            exAgent.SetCellValue(x, y + 3, qty); 
                        }
                        #endregion
                        
                            
                        
                        flgBreak = true;
                    }
                } 
                HideNullRows(page); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }
        private void HideNullRows(int pages)
        {
            try
            {
                exAgent.HideRows((pages+1)*25+1, 1, 500, 999);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public void GetHLTVV06Report(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "VV06";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath =   strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\发货单\" + WBBAUFKRS + WBBAUFLFD + ".xls";
                        sheetName = WBBALIEFSCH.Trim();
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        } 
    }
}
